package com.ludomancy.starwave;

import java.awt.Graphics;
import java.util.Iterator;
import java.util.Vector;

import com.ludomancy.gamelib.ImageAnimation;
import com.ludomancy.gamelib.Sound;

public class AttackAnimation implements Animation
{
	public Ship attacker;
	public int kickback = -Settings.ATTACK_SHIP_KICKBACK_DEPTH;
	public Vector enemies;
	public ImageAnimation[] explosionAnimations;
	public boolean explosionsOver = false;
	public boolean started = false;
	private int currentKickbackFrame = 0;
	
	public AttackAnimation(Ship attacker, Vector enemies)
	{
		this.attacker = attacker;
		this.enemies = enemies;
		if(enemies.isEmpty() || !attacker.isShooter())
		{
			kickback = 0;
		}
	}
	
	private boolean enemiesAreDead()
	{
		boolean dead = true;
		for (Iterator iter = enemies.iterator(); iter.hasNext();)
		{
			Ship enemy = (Ship) iter.next();
			if(!enemy.isDead())
			{
				dead = false;
			}
		}
		return dead;
	}
	
	public void start()
	{
		started = true;
	}
	
	public void update()
	{
		if(started)
		{
			if(explosionAnimations == null)
			{
				explosionAnimations = new ImageAnimation[enemies.size()];
				for (int i = 0; i < explosionAnimations.length; i++)
				{
					explosionAnimations[i] = new ImageAnimation(Settings.getExplosion(attacker.owner.direction), Settings.EXPLOSION_FRAME_TIME);
					explosionAnimations[i].play();
					// explosion sound
					new Sound("blow.wav").play();
				}
			}
			if(currentKickbackFrame == Settings.ATTACK_SHIP_KICKBACK_FRAME_TIME)
			{
				kickback = Math.min(0, kickback+1);
				currentKickbackFrame = 0;
			}
			else
			{
				currentKickbackFrame++;
			}
			
			explosionsOver = true;
			for (int i = 0; i < explosionAnimations.length; i++)
			{
				explosionAnimations[i].update();
				if(!explosionAnimations[i].isOver())
				{
					explosionsOver = false;
				}
			}
			
			if(explosionsOver)
			{
				// do attacks
				for (Iterator iter = enemies.iterator(); iter.hasNext();)
				{
					Ship enemy = (Ship) iter.next();
					attacker.shoot(enemy);
				}
				started = false;
			}
			if(enemiesAreDead())
			{
				kickback = 0;
				explosionsOver = true;
			}
		}
	}
	
	public boolean isOver()
	{
		return kickback == 0 && (enemies.isEmpty() || explosionsOver);
	}
	
	public void paint(Graphics g)
	{
		int offsetX = 0;
		if(started)
		{
			offsetX = kickback*attacker.owner.direction;
		}
		if(!started || attacker.isShooter())
		{
			if(attacker.isDead() && enemies.isEmpty())
			{
				attacker.paintDead(g, attacker.px*Settings.TILE_SIZE+offsetX, attacker.py*Settings.TILE_SIZE);
			}
			else
			{
				attacker.paint(g, attacker.px*Settings.TILE_SIZE+offsetX, attacker.py*Settings.TILE_SIZE);
			}
		}
		if(explosionAnimations != null)
		{
			for (int i = 0; i < explosionAnimations.length; i++)
			{
				Ship enemy = (Ship) enemies.elementAt(i);
				if(!explosionAnimations[i].isOver() && !enemy.isDead())
				{
					explosionAnimations[i].paintCurrentFrame(g, enemy.px*Settings.TILE_SIZE, enemy.py*Settings.TILE_SIZE);
				}
			}
		}
	}
}
